package com.example.fileserver_API.Globa;


import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.utils.APIResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 全局异常拦截，鉴权失败不会报错，会返回给前端报错原因
     * @param e 错误
     * @return APIResult
     */
    @ExceptionHandler
    public APIResult handlerException(Exception e) {
        logger.error(e.getMessage());
        if (e.getMessage().equals("请登录")){
            return APIResultUtils.error("请重新登录");
        } else if (e.getMessage().equals("您的权限不足")) {
            return APIResultUtils.error(e.getMessage(),2);
        }
        return APIResultUtils.error("发生错误"+e.getMessage(),e.getMessage());
    }
}
